package com.ufo.store.management.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

import com.ufo.store.management.R;
import com.ufo.store.management.adapter.PayMasterExpandAdapter;
import com.ufo.store.management.entity.PayMaster;
import com.ufo.store.management.entity.PayMasterGroup;
import com.ufo.store.management.entity.SerializableList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/10/4.
 */
public class PayListActivity extends BaseActivity {
    private ExpandableListView mPayMasterListElv;
    private PayMasterExpandAdapter statusAdapter;
    private LinearLayout mFailLl;
    private List<PayMasterGroup> mPayMasterGroupInfoList = new ArrayList<PayMasterGroup>();

    private String mSearchDate = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle("销售列表");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back_selector);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                SerializableList p = (SerializableList) bundle.getSerializable("PayMasterGroup");
                mPayMasterGroupInfoList = p.getmPayMasterGroupInfoList();
                mSearchDate = bundle.getString("Date");
            }
        }
    }

    private void initView() {
        setContentView(R.layout.activity_paylist);
        mFailLl = (LinearLayout) findViewById(R.id.get_fail_tip_ly);
        mPayMasterListElv = (ExpandableListView) findViewById(R.id.expandlist);
        statusAdapter = new PayMasterExpandAdapter(this, mPayMasterGroupInfoList);
        mPayMasterListElv.setAdapter(statusAdapter);
        mPayMasterListElv.setSelection(0);
        // 去掉ExpandableListView 默认的箭头 
        mPayMasterListElv.setGroupIndicator(null);
        int groupCount = mPayMasterListElv.getCount();
        // 首次加载展开第一项
        for( int i = 0; i < groupCount; i++ ){
            mPayMasterListElv.expandGroup(i);
        }
        // return true不能点击收缩   return false 可以点击收缩
        mPayMasterListElv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });
        mPayMasterListElv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                PayMaster payMaster = mPayMasterGroupInfoList.get(groupPosition).getChildList().get(childPosition);
                Intent toIntent = new Intent(PayListActivity.this, SaleDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("Billno", payMaster.payMasterBillno);
                toIntent.putExtras(bundle);
                startActivity(toIntent);
                return true;
            }
        });

        if(mPayMasterGroupInfoList.size() != 0 ){
            mFailLl.setVisibility(View.GONE);
            mPayMasterListElv.setVisibility(View.VISIBLE);
        }else{
            mFailLl.setVisibility(View.VISIBLE);
            mPayMasterListElv.setVisibility(View.GONE);
        }
    }
}
